﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FortuneHeavenBAL;
using FortuneHeavenCommon.DataMap;
using FortuneHeavenCommon.ComMethod;
using FortuneHeavenCommon.Enum;

namespace FortuneHeaven.Admin
{
    public partial class ChargeCard : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public void ShowMessage(string infor)
        {
            Response.Write(string.Format("<script>alert('{0}');</script>", infor));
        }

        public void LoginCheck(object sender, EventArgs e)
        {
            string username =this.TB_UserName.Text.Trim();
            string doublename =TB_DBUserName.Text.Trim();
            string validatecode =this.NTB_ValidateCode.Text.Trim();
            string chargecode = this.TB_Card.Text.Trim();
            DateTime time = DateTime.Now;
            try
            {
                if (Session[ValidationCode.Name] == null)
                {
                    ShowMessage("验证码错误");
                    return;
                }
                if (validatecode.ToLower() != ((string)WebSession.Session[ValidationCode.Name]).ToLower())
                {
                    ShowMessage("验证码错误");
                    return;
                }

                if (username != doublename)
                {
                    ShowMessage("用户名两次输入不同");
                    return;
                }
                AccountsInfoBO bo = new AccountsInfoBO();
                AccountsInfo acount = bo.GetLightAcountInfor(username);
                if (acount == null)
                {
                    ShowMessage("该用户名不存在");
                    return;
                }
                RechargeCodeBO re = new RechargeCodeBO();
                RechargeCodeRecord record = re.GetRechargeCode(CommonEncrypt.Encode(chargecode));
                if (record == null)
                {
                    ShowMessage("该充值卡无效");
                    return;
                }
                if (record.Status == 1)
                {
                    ShowMessage("充值卡已无效");
                    return;
                }

                record.Status = 1;
                record.UsedTime = time;
                record.UserID = acount.UserID;
                re.Save(record);

                //UserFortuneBO fortunebo = new UserFortuneBO();
                //UserFortune fortune = fortunebo.GetUserFortune(acount.UserID);
                //if (fortune == null)
                //{
                //    fortune = new UserFortune();
                //    fortune.UserID = acount.UserID;
                //    fortune.CurrentValue = record.RechargeValue;
                //    fortune.UpdateTime = time;
                //    fortunebo.Add(fortune);
                //}
                //else
                //{
                //    fortune.CurrentValue += record.RechargeValue;
                //    fortune.UpdateTime = time;
                //    fortunebo.Save(fortune);
                //}
                int score = (int)(record.RechargeValue * 50);
                //GameScoreInfoBO gamesore = new GameScoreInfoBO();
                //if (!gamesore.AddScore(score, acount.UserID)) {
                //    throw new Exception("冲积分出现问题");
                //}
                UserInforBO userbo = new UserInforBO();
                UserInfor customer = userbo.GetUserInfor(acount.UserID);
                if (customer == null)
                {
                    customer = new UserInfor();
                    customer.UserID = acount.UserID;
                }
                customer.FortuneValue = customer.FortuneValue + record.RechargeValue;
                userbo.SaveUserInfor(customer);

                //如果有推广员，则添加推广积分
                if (acount.SpreaderID > 0)
                {
                  
                    AccountsInfo target = bo.GetAcount(acount.SpreaderID);
                    if (target != null)
                    {
                        UserInfor user = userbo.GetUserInfor(acount.SpreaderID);
                        //推广积分换算比例
                        int spreadvalue = (int)(score * 0.1);
                        if (user != null)
                        {  
                            user.SpreadValue = user.SpreadValue + spreadvalue;
                        }
                        else
                        {
                            user = new UserInfor();
                            user.UserID = acount.SpreaderID;
                            user.SpreadValue = spreadvalue;
                        }
                        userbo.SaveUserInfor(user);
                        UserSpreadBO spreadrecord = new UserSpreadBO();
                        UserSpread sp = new UserSpread();
                        sp.UserID = target.UserID;
                        sp.FromUserID = acount.UserID;
                        sp.FromUserName = acount.Accounts;
                        sp.RechargeType = (int)SpreadType.charge;
                        sp.Remark = "推广用户使用充值卡";
                        sp.Value = spreadvalue;
                        sp.DateTime = time;
                        sp.ID = spreadrecord.GetMaxID();
                        spreadrecord.Save(sp, true);
                    }
                }

                RechargeHistoryBO history = new RechargeHistoryBO();
                RechargeHistory his = new RechargeHistory();
                his.RechargeValue = record.RechargeValue;
                his.CardCode = record.RechargeCode;
                his.RechargeType =(int)RechargeType.Card;
                his.Time = time;
                his.UserID = acount.UserID;
                history.Add(his);
                ShowMessage("充值成功");
            }
            catch (Exception exp)
            {
                Response.Clear();
                Response.Write("服务器异常！请稍后提交或者联系公司");
                Response.End();
            }
        }
    }
}